package com.yoyo.yoyoplat.sdk;

import android.content.Context;
import android.content.Intent;

import com.dollkey.hdownload.util.HRxBus;
import com.yoyo.yoyoplat.base.ApiConstants;
import com.yoyo.yoyoplat.bean.ARequestBean;
import com.yoyo.yoyoplat.bean.AdInfoBean;
import com.yoyo.yoyoplat.bean.DeviceDTO;
import com.yoyo.yoyoplat.bean.ImpDTO;
import com.yoyo.yoyoplat.bean.DRequestBean;
import com.yoyo.yoyoplat.util.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

import static com.yoyo.yoyoplat.base.ApiConstants.ACTION_DLE_START;
import static com.yoyo.yoyoplat.base.ApiConstants.ACTION_DLE_SUCCESS;
import static com.yoyo.yoyoplat.base.ApiConstants.ACTION_INSTALL_START;

public class YYNative {
    private final Observable<String> mRegister;
    private final Disposable mDisposable;
    private Context mContext;
    private DRequestBean mRequestBean;
    private ARequestBean mARequestBean;
    private YoYoNativeListener mYoYoNativeListener;
    //    private AppInstallReceiver mAppInstallReceiver;
    private NativeResponseImpl mNativeResponse;

    //sizeType 10001
    public YYNative(Context context, int impId, int sizeType) {
        //注册接收异常广播消息
        mContext = context;
//        mAppInstallReceiver = new AppInstallReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
//        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
//        context.registerReceiver(mAppInstallReceiver, filter);
        mRegister = HRxBus.getInstance().register(HRxBus.RX_ACTION_INSTALL);
        mDisposable = mRegister.subscribe(new Consumer<String>() {
            @Override
            public void accept(String o) {
                //接收安装广播
                switch (o) {
                    case ACTION_DLE_START:
                        mNativeResponse.dot(ApiConstants.TK_DLE_START);
                        break;
                    case ACTION_DLE_SUCCESS:
                        mNativeResponse.dot(ApiConstants.TK_DLE_SUCCESS);
                        break;
                    case ACTION_INSTALL_START:
                        mNativeResponse.dot(ApiConstants.TK_INSTALL_START);
                        break;
                    case Intent.ACTION_PACKAGE_ADDED:
                    case Intent.ACTION_PACKAGE_REPLACED:
                        mNativeResponse.dot(ApiConstants.TK_INSTALL_SUCCESS);
                        break;
                }
            }
        });
        DeviceDTO deviceDTO = new DeviceDTO(context);
        mRequestBean = new DRequestBean();
        mRequestBean.setDevice(deviceDTO);
        mRequestBean.setImp(new ImpDTO(ApiConstants.BUSINESS_TYPE_DRAW, impId, sizeType));

        mARequestBean = new ARequestBean();
        mARequestBean.setDevice(deviceDTO);
    }

    public void destroy() {
        HRxBus.getInstance().unregister(HRxBus.RX_ACTION_INSTALL, mRegister);
        if (mDisposable != null && !mDisposable.isDisposed())
            mDisposable.dispose();
//        mContext.unregisterReceiver(mAppInstallReceiver);
    }

    public void setYoYoNativeListener(YoYoNativeListener yoYoNativeListener) {
        mYoYoNativeListener = yoYoNativeListener;
    }

    public void loadData() {
        HttpUtils.doHttpRequest(ApiConstants.POST, ApiConstants.getData, ApiConstants.getUrlMap(),
                mRequestBean, AdInfoBean.class, new HttpUtils.ObjectCallback<AdInfoBean>() {
                    @Override
                    public void onSuccess(AdInfoBean adInfoBean) {
                        if (adInfoBean.getCode().equals(ApiConstants.SUCCESS)) {
                            if (mYoYoNativeListener != null) {
                                List<YYNativeResponse> list = new ArrayList<>(adInfoBean.getData().getAdList().size());
                                for (AdInfoBean.DO a : adInfoBean.getData().getAdList()) {
                                    YYNativeResponse yyNativeResponse = new NativeResponseImpl(a, mARequestBean, new NativeResponseImpl.ChooseByClickListener() {
                                        @Override
                                        public void onChoose(NativeResponseImpl nativeResponse) {
                                            mNativeResponse = nativeResponse;
                                        }
                                    });
                                    list.add(yyNativeResponse);
                                }
                                mYoYoNativeListener.success(list);
                            }
                        } else {
                            if (mYoYoNativeListener != null) {
                                mYoYoNativeListener.fail(adInfoBean.getCode(), adInfoBean.getError());
                            }
                        }
                    }

                    @Override
                    public void onFailure(int code, Exception e) {
                        if (mYoYoNativeListener != null) {
                            mYoYoNativeListener.fail(String.valueOf(code), e.getCause().getMessage());
                        }
                    }
                });
    }

//    private class AppInstallReceiver extends BroadcastReceiver {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent.getAction() == null) {
//                return;
//            }
//            //接收安装广播
//            switch (intent.getAction()) {
//                case ACTION_DLE_START:
//                    mNativeResponse.dot(ApiConstants.TK_DLE_START);
//                    break;
//                case ACTION_DLE_SUCCESS:
//                    mNativeResponse.dot(ApiConstants.TK_DLE_SUCCESS);
//                    break;
//                case ACTION_INSTALL_START:
//                    mNativeResponse.dot(ApiConstants.TK_INSTALL_START);
//                    break;
//                case Intent.ACTION_PACKAGE_ADDED:
//                case Intent.ACTION_PACKAGE_REPLACED:
////                    String packageName = intent.getDataString();
//                    mNativeResponse.dot(ApiConstants.TK_INSTALL_SUCCESS);
//                    break;
//            }
//        }
//    }

    public interface YoYoNativeListener {
        void success(List<YYNativeResponse> data);

        void fail(String code, String err);
    }

}
